/* =============================================================
 * Define the functions of
 *            class Mesh_Info
 *
 * This is the function routine that 
 *        1. Construct & Destruct the Mesh_Info class
 *        2. Read Mesh infomation form .dat file.
 *
 *        and more ...
 *
 * Author: Ju Liu, Sept 14th 2011
 *         C.S.E.M. student, The Univ of Texas
 * ===========================================================*/

#include "Mesh_Info_class.hpp"

Mesh_Info::Mesh_Info()
{
  p_x = 0;
  p_y = 0;
  n_x = 0;
  n_y = 0;
  per_x = 0;
  per_y = 0;
  Xi_x = vector<double>();
  Xi_y = vector<double>();
  x = vector<double>();
  y = vector<double>();
  w = vector<double>();
};


void Mesh_Info::InputMesh( const char file_path[] )
{
  int d;
  string word;
  ifstream meshfile;

  cout<<"Start reading the meshfile... ..."<<endl;

  meshfile.open( file_path );

  meshfile >> word >> d;

  if(d !=2 )
  {
    cerr<<"     ERROR: DIMENSION OF THE MESH FILE IS NOT EQUAL TO 2."<<endl;
    exit(1);
  }

  meshfile >> word >> word >> word >> word >> p_x;
  meshfile >> word >> word >> word >> word >> p_y;

  meshfile >> word >> word >> word >> word >> word >> n_x;
  meshfile >> word >> word >> word >> word >> word >> n_y;

  meshfile >> word >> word >> word >> per_x;
  meshfile >> word >> word >> word >> per_y;
  
  int num_knots_x = n_x + p_x + 1;
  int num_knots_y = n_y + p_y + 1;

  Xi_x.resize(num_knots_x);
  Xi_y.resize(num_knots_y);

  int num_funcs = n_x * n_y;
  
  x.resize(num_funcs);
  y.resize(num_funcs);
  w.resize(num_funcs);

  meshfile >> word >> word >> word >> word;
  for(int i=0;i<num_knots_x; i++)
    meshfile>>Xi_x[i];

  meshfile >> word >> word >> word >> word;
  for(int i=0;i<num_knots_y; i++)
      meshfile>>Xi_y[i];


  meshfile >> word >> word >> word >> word;
  for(int i=0; i<num_funcs; i++)
    meshfile >> x[i] >> y[i] >> w[i];

  cout<<"Finish reading meshfile."<<endl;
  meshfile.close();
  
};
